import Vue from 'vue'
import VueRouter from 'vue-router'
import SeekerView from '../views/SeekerView.vue'
import Login from '../views/LoginView.vue'
import MessageView from '../components/message/Message.vue'
import SeekerHome from '../components/seekerHome/SeekerHome.vue'
import Recommend from '@/components/recommend/Recommend.vue'
import SeekerSearch from '../components/seekerSearch/SeekerSearch.vue'
import SeekerInterview from '../components/seekerInterview/SeekerInterview.vue'
import SeekerResume from '../components/seekerResume/SeekerResume.vue'
import enterpriseView from "../views/EnterpriseView.vue";
import OccupationManage from "@/components/occupationInfoMg/OccupationManage.vue";
import AddOccupation from "@/components/occupationInfoMg/AddOccupation.vue";
import EnterpriseInfoShow from "@/components/enterpriseInfoMg/EnterpriseInfoShow.vue";
import EnterpriseInfoManage from "@/components/enterpriseInfoMg/EnterpriseInfoManage.vue";
import EnterpriseInterview from "@/components/enterpriseInterview/EnterpriseInterview.vue";
import SelectInterview from "@/components/enterpriseInterview/SelectInterview.vue";
import InsertInterview from "@/components/enterpriseInterview/InsertInterview.vue";
import Error from '@/views/Error.vue'
import axios from "axios";

import ResumeDelivery from "@/components/delivery/ResumeDelivery.vue";

Vue.use(VueRouter)

const routes = [
    {
        // 登录页
        path: '/login',
        // 路由跳转路径
        name: 'Login',
        // 路由名称
        component: Login
        // 路由跳转组件
    },
    {
        //失效页
        path: '/error',
        name: 'Error',
        component: Error
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        // 求职者页
        path: '/seekerView',
        name: 'SeekerView',
        component: SeekerView,
        children: [
            {
                //首页
                path: '/seekerHome/SeekerHome',
                name: 'SeekerHome',
                component: SeekerHome
            },
            {
                //推荐页
                path: '/recommend/Recommend',
                name: 'Recommend',
                component: Recommend
            },
            {
                //搜索页
                path: '/seekerSearch/SeekerSearch',
                name: 'SeekerSearch',
                component: SeekerSearch
            },
            {
                //消息页
                path: '/message/MessageView',
                name: 'MessageView',
                component: MessageView
            },
            {
                //面试页
                path: '/seekerInterview/SeekerInterView',
                name: 'SeekerInterView',
                component: SeekerInterview
            },
            {
                //简历页
                path: '/seekerResume/SeekerResume',
                name: 'SeekerResume',
                component: SeekerResume
            },
            {
                path: '/delver/ResumeDelivery',
                name: 'ResumeDelivery',
                component: ResumeDelivery
            }
        ]
    },
    {
        //企业界面
        // 路由跳转路径
        path: '/enterpriseView',
        // 路由名称
        name: 'EnterpriseView',
        // 路由跳转组件
        component: enterpriseView,
        children: [
            {
                //企业发布职位界面
                path: '/occupationInfoMg/AddOccupation',
                name: 'AddOccupation',
                component: AddOccupation
            },
            {
                //消息页
                path: '/message/MessageView1',
                name: 'MessageView',
                component: MessageView
            },
            {
                //企业管理展示界面
                path: '/enterpriseInfoMg/EnterpriseInfoShow',
                name: 'EnterpriseInfoShow',
                component: EnterpriseInfoShow,
                children: [
                    {
                        //企业管理职位界面(在企业管理界面中使用)
                        path: '/occupationInfoMg/OccupationManage',
                        name: 'OccupationManage',
                        component: OccupationManage
                    },
                ]
            },
            {
                //企业基本信息管理界面
                path: '/enterpriseInfoMg/EnterpriseInfoManage',
                name: 'EnterpriseInfoManage',
                component: EnterpriseInfoManage
            },
            {
                //企业面试
                path: '/enterpriseInterview/EnterpriseInterview',
                name: 'EnterpriseInterview',
                component: EnterpriseInterview,
                children: [
                    {
                        path: '/enterpriseInterview/SelectInterview',
                        name: 'SelectInterview',
                        component: SelectInterview
                    },
                    {
                        path: '/enterpriseInterview/InsertInterview',
                        name: 'InsertInterview',
                        component: InsertInterview
                    }
                ]
            }
        ],
    },

]

const router = new VueRouter({
    // mode:history,
    routes: routes
})

router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/login')) {
        window.localStorage.removeItem('access-od')
        next()
    } else {
        let od = JSON.parse(window.localStorage.getItem('access-od'))
        if (!od) {
            next({ path: '/login' })
        } else {
            axios.get('/checkToken', {
                headers: {
                    token: od.token
                },
            }).then((resp) => {
                if (resp.data === false) {
                    console.log("校验失败")
                    next({ path: '/error' })
                }
            })
            next()
        }
    }
})

export default router
